package com.fanxuankai.boot.mqbroker.producer.autoconfigure;

import com.fanxuankai.boot.mqbroker.Event;
import com.fanxuankai.boot.mqbroker.producer.AbstractMqProducer;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;

/**
 * @author fanxuankai
 */
@ConditionalOnClass({RocketMQTemplate.class})
@ConditionalOnProperty(name = MqBrokerProducerProperties.ENABLED, matchIfMissing = true)
public class MqBrokerProducerRocketAutoConfiguration {
    @Bean
    @ConditionalOnBean
    public AbstractMqProducer mqProducer(RocketMQTemplate rocketTemplate) {
        return new AbstractMqProducer() {
            @Override
            public boolean isPublisherCallback() {
                return false;
            }

            @Override
            public void accept(Event<String> event) {
                rocketTemplate.convertAndSend(event.getName(), event);
            }
        };
    }
}
